<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.1.1">
<meta name="baidu-site-verification" content="code-hkfwkpUecU" />
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="baidu-site-verification" content="true">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Monda:300,300italic,400,400italic,700,700italic|Roboto+Slab:300,300italic,400,400italic,700,700italic|PT+Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.14.0/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"github.com","root":"/","scheme":"Gemini","version":"8.0.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"disqus","storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>

  <meta name="description" content="操作系统接口 os 模块提供了很多与操作系统交互的函数 12import osprint(os.getcwd()) # 当前目录路径  只能使用 import os 而不是 from os  import *，这将避免内建的 open() 函数被 os.open() 隐式替换掉  内置的 dir() 和 help() 函数可用作交互式辅助工具，用于处理大型模块，如 os 123&gt;&gt;&amp;g">
<meta property="og:type" content="article">
<meta property="og:title" content="标准库">
<meta property="og:url" content="https://github.com/shanlu123/2020/09/30/%E6%95%B0%E6%8D%AE%E5%BA%93/%E6%A0%87%E5%87%86%E5%BA%93/index.html">
<meta property="og:site_name" content="shanlu的博客">
<meta property="og:description" content="操作系统接口 os 模块提供了很多与操作系统交互的函数 12import osprint(os.getcwd()) # 当前目录路径  只能使用 import os 而不是 from os  import *，这将避免内建的 open() 函数被 os.open() 隐式替换掉  内置的 dir() 和 help() 函数可用作交互式辅助工具，用于处理大型模块，如 os 123&gt;&gt;&amp;g">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-09-29T16:00:00.000Z">
<meta property="article:modified_time" content="2020-10-04T09:05:21.084Z">
<meta property="article:author" content="shanlu">
<meta property="article:tag" content="python">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://github.com/shanlu123/2020/09/30/%E6%95%B0%E6%8D%AE%E5%BA%93/%E6%A0%87%E5%87%86%E5%BA%93/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>标准库 | shanlu的博客</title>
  






  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>
  <a href="https://github.com/shanlu123" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">shanlu的博客</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">三餐四季，冷暖自知</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/home" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-前端">

    <a href="/%E5%89%8D%E7%AB%AF/" rel="section"><i class="fa fa-cloud fa-fw"></i>前端</a>

  </li>
        <li class="menu-item menu-item-生活">

    <a href="/%E7%94%9F%E6%B4%BB/" rel="section"><i class="fa fa-user fa-fw"></i>生活</a>

  </li>
        <li class="menu-item menu-item-school">

    <a href="/school/" rel="section"><i class="fa fa-graduation-cap fa-fw"></i>学校</a>

  </li>
        <li class="menu-item menu-item-tree_hole">

    <a href="/tree_hole/" rel="section"><i class="fa fa-tree fa-fw"></i>树洞</a>

  </li>
        <li class="menu-item menu-item-photos">

    <a href="/photos/" rel="section"><i class="fa fa-camera fa-fw"></i>相册</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <section class="post-toc-wrap sidebar-panel">
          <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E6%8E%A5%E5%8F%A3"><span class="nav-number">1.</span> <span class="nav-text">操作系统接口</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%87%E4%BB%B6%E9%80%9A%E9%85%8D%E7%AC%A6"><span class="nav-number">2.</span> <span class="nav-text">文件通配符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%8F%82%E6%95%B0"><span class="nav-number">3.</span> <span class="nav-text">命令行参数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%94%99%E8%AF%AF%E8%BE%93%E5%87%BA%E9%87%8D%E5%AE%9A%E5%90%91%E5%92%8C%E7%A8%8B%E5%BA%8F%E7%BB%88%E6%AD%A2"><span class="nav-number">4.</span> <span class="nav-text">错误输出重定向和程序终止</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D"><span class="nav-number">5.</span> <span class="nav-text">字符串模式匹配</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%95%B0%E5%AD%A6"><span class="nav-number">6.</span> <span class="nav-text">数学</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BA%92%E8%81%94%E7%BD%91%E8%AE%BF%E9%97%AE"><span class="nav-number">7.</span> <span class="nav-text">互联网访问</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%97%A5%E6%9C%9F%E5%92%8C%E6%97%B6%E9%97%B4"><span class="nav-number">8.</span> <span class="nav-text">日期和时间</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%95%B0%E6%8D%AE%E5%8E%8B%E7%BC%A9"><span class="nav-number">9.</span> <span class="nav-text">数据压缩</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%80%A7%E8%83%BD%E6%B5%8B%E9%87%8F"><span class="nav-number">10.</span> <span class="nav-text">性能测量</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%B4%A8%E9%87%8F%E6%8E%A7%E5%88%B6"><span class="nav-number">11.</span> <span class="nav-text">质量控制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%87%AA%E5%B8%A6%E7%94%B5%E6%B1%A0"><span class="nav-number">12.</span> <span class="nav-text">自带电池</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A0%BC%E5%BC%8F%E5%8C%96%E8%BE%93%E5%87%BA"><span class="nav-number">13.</span> <span class="nav-text">格式化输出</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A8%A1%E6%9D%BF"><span class="nav-number">14.</span> <span class="nav-text">模板</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8%E4%BA%8C%E8%BF%9B%E5%88%B6%E6%95%B0%E6%8D%AE%E8%AE%B0%E5%BD%95%E6%A0%BC%E5%BC%8F"><span class="nav-number">15.</span> <span class="nav-text">使用二进制数据记录格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%9A%E7%BA%BF%E7%A8%8B"><span class="nav-number">16.</span> <span class="nav-text">多线程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%97%A5%E5%BF%97%E8%AE%B0%E5%BD%95"><span class="nav-number">17.</span> <span class="nav-text">日志记录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BC%B1%E5%BC%95%E7%94%A8"><span class="nav-number">18.</span> <span class="nav-text">弱引用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%94%A8%E4%BA%8E%E6%93%8D%E4%BD%9C%E5%88%97%E8%A1%A8%E7%9A%84%E5%B7%A5%E5%85%B7"><span class="nav-number">19.</span> <span class="nav-text">用于操作列表的工具</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8D%81%E8%BF%9B%E5%88%B6%E6%B5%AE%E7%82%B9%E6%95%B0%E8%BF%90%E7%AE%97"><span class="nav-number">20.</span> <span class="nav-text">十进制浮点数运算</span></a></li></ol></div>
      </section>
      <!--/noindex-->

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="shanlu"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">shanlu</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">127</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/shanlu123" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;shanlu123"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.cnblogs.com/shanlu0000/p/11478994.html" title="博客园 → https:&#x2F;&#x2F;www.cnblogs.com&#x2F;shanlu0000&#x2F;p&#x2F;11478994.html" rel="noopener" target="_blank"><i class="fa fa-blog fa-fw"></i>博客园</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:yourname@gmail.com" title="E-Mail → mailto:yourname@gmail.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/u/6662419816/home?wvr=5" title="微博 → https:&#x2F;&#x2F;weibo.com&#x2F;u&#x2F;6662419816&#x2F;home?wvr&#x3D;5" rel="noopener" target="_blank"><i class="fab fa-weibo fa-fw"></i>微博</a>
      </span>
  </div>


  <div class="links-of-blogroll animated">
    <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://www.npmjs.com/" title="https:&#x2F;&#x2F;www.npmjs.com&#x2F;" rel="noopener" target="_blank">npm</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://www.bilibili.com/" title="https:&#x2F;&#x2F;www.bilibili.com&#x2F;" rel="noopener" target="_blank">bilibili</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://stackoverflow.com/" title="https:&#x2F;&#x2F;stackoverflow.com&#x2F;" rel="noopener" target="_blank">stackoverflow</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://developers.weixin.qq.com/miniprogram/dev/component/" title="https:&#x2F;&#x2F;developers.weixin.qq.com&#x2F;miniprogram&#x2F;dev&#x2F;component&#x2F;" rel="noopener" target="_blank">小程序</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://www.ruanyifeng.com/blog/" title="http:&#x2F;&#x2F;www.ruanyifeng.com&#x2F;blog&#x2F;" rel="noopener" target="_blank">阮一峰</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://cn.aliyun.com/" title="https:&#x2F;&#x2F;cn.aliyun.com&#x2F;" rel="noopener" target="_blank">阿里云</a>
        </li>
    </ul>
  </div>

      </section>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">
      

      

    
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/shanlu123/2020/09/30/%E6%95%B0%E6%8D%AE%E5%BA%93/%E6%A0%87%E5%87%86%E5%BA%93/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="shanlu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="shanlu的博客">
    </span>

    
    
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          标准库
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-09-30 00:00:00" itemprop="dateCreated datePublished" datetime="2020-09-30T00:00:00+08:00">2020-09-30</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2020-10-04 17:05:21" itemprop="dateModified" datetime="2020-10-04T17:05:21+08:00">2020-10-04</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/python/" itemprop="url" rel="index"><span itemprop="name">python</span></a>
        </span>
    </span>

  
    <span id="/2020/09/30/%E6%95%B0%E6%8D%AE%E5%BA%93/%E6%A0%87%E5%87%86%E5%BA%93/" class="post-meta-item leancloud_visitors" data-flag-title="标准库" title="阅读次数">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span class="leancloud-visitors-count"></span>
    </span>
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2020/09/30/%E6%95%B0%E6%8D%AE%E5%BA%93/%E6%A0%87%E5%87%86%E5%BA%93/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2020/09/30/%E6%95%B0%E6%8D%AE%E5%BA%93/%E6%A0%87%E5%87%86%E5%BA%93/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>7.6k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>7 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h3 id="操作系统接口"><a href="#操作系统接口" class="headerlink" title="操作系统接口"></a>操作系统接口</h3><ul>
<li><p>os 模块提供了很多与操作系统交互的函数</p>
<figure class="highlight moonscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="built_in">os</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">os</span>.getcwd()) # 当前目录路径</span><br></pre></td></tr></table></figure>

<p>只能使用 import os 而不是 from os  import *，这将避免内建的 open() 函数被 os.open() 隐式替换掉</p>
</li>
<li><p>内置的 dir() 和 help() 函数可用作交互式辅助工具，用于处理大型模块，如 os</p>
<figure class="highlight python-repl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt;</span> <span class="python"><span class="keyword">import</span> os</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt;</span> <span class="python">dir(os)  <span class="comment"># 模块列表</span></span></span><br><span class="line"><span class="meta">&gt;&gt;&gt;</span> <span class="python">help(os) </span></span><br></pre></td></tr></table></figure>
</li>
<li><p>对于日常文件和目录管理任务，shutil 模块提供了更易于使用的更高级别的接口</p>
</li>
</ul>
<h3 id="文件通配符"><a href="#文件通配符" class="headerlink" title="文件通配符"></a>文件通配符</h3><ul>
<li><p>glob 模块提供了一个在目录中使用通配符搜索创建文件列表的函数</p>
<figure class="highlight python-repl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt;</span> <span class="python"><span class="keyword">import</span> glob</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt;</span> <span class="python">glob.glob(<span class="string">&#x27;*.py&#x27;</span>)   <span class="comment"># 找到所有py文件列表</span></span></span><br></pre></td></tr></table></figure>

</li>
</ul>
<h3 id="命令行参数"><a href="#命令行参数" class="headerlink" title="命令行参数"></a>命令行参数</h3><ul>
<li>这些参数作为列表存储在 sys 模块的 argv 属性中</li>
<li>argparse 模块提供了一种更加复杂的机制来处理命令行参数</li>
</ul>
<h3 id="错误输出重定向和程序终止"><a href="#错误输出重定向和程序终止" class="headerlink" title="错误输出重定向和程序终止"></a>错误输出重定向和程序终止</h3><ul>
<li>sys 模块还有 stdin，stdout 和 stderr 的属性</li>
<li>stderr 对于发出警告和错误消息非常有用，即使在 stdout 被重定向后也可以看到它们</li>
<li>终止脚本最直接的方法是使用 sys.exit()</li>
</ul>
<h3 id="字符串模式匹配"><a href="#字符串模式匹配" class="headerlink" title="字符串模式匹配"></a>字符串模式匹配</h3><ul>
<li><p>re 模块为高级字符串处理提供正则表达式工具</p>
<figure class="highlight python-repl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt;</span> <span class="python"><span class="keyword">import</span> re</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt;</span> <span class="python">re.findall(<span class="string">r&#x27;\bf[a-z]*&#x27;</span>,<span class="string">&#x27;which foot or hand fell faster&#x27;</span>)</span></span><br><span class="line">[&#x27;foot&#x27;, &#x27;fell&#x27;, &#x27;faster&#x27;]</span><br><span class="line"><span class="meta">&gt;&gt;&gt;</span> <span class="python">re.sub(<span class="string">r&#x27;(\b[a-z]+) \1&#x27;</span>,<span class="string">r&#x27;\1&#x27;</span>,<span class="string">&#x27;cat in the the hat&#x27;</span>)</span></span><br><span class="line">&#x27;cat in the hat&#x27;</span><br><span class="line"><span class="meta">&gt;&gt;&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>当只需要简单的功能时，首选字符串方法更容易阅读和调试</p>
<figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;</span>&gt; <span class="string">&#x27;tea for too&#x27;</span>.replace(<span class="string">&#x27;too&#x27;</span>,<span class="string">&#x27;two&#x27;</span>)</span><br><span class="line"><span class="string">&#x27;tea for two&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;</span>&gt;</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h3 id="数学"><a href="#数学" class="headerlink" title="数学"></a>数学</h3><ul>
<li><p>math 模块提供对浮点数的底层C库函数的访问</p>
<figure class="highlight gauss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">import math</span><br><span class="line"></span><br><span class="line"><span class="keyword">print</span>(math.<span class="built_in">cos</span>(math.<span class="built_in">pi</span> / <span class="number">4</span>))  <span class="meta"># 0.7071067811865476</span></span><br><span class="line"><span class="keyword">print</span>(math.<span class="built_in">log</span>(<span class="number">1024</span>, <span class="number">2</span>))  <span class="meta"># 10.0</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>random 模块提供了进行随机选择的工具</p>
<figure class="highlight angelscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> random</span><br><span class="line"></span><br><span class="line">r1 = random.choice([<span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;pear&#x27;</span>, <span class="string">&#x27;banana&#x27;</span>])</span><br><span class="line">print(r1)  # pear</span><br><span class="line"></span><br><span class="line">r2 = random.sample(range(<span class="number">100</span>), <span class="number">10</span>)</span><br><span class="line">print(r2)  # [<span class="number">20</span>, <span class="number">63</span>, <span class="number">64</span>, <span class="number">25</span>, <span class="number">13</span>, <span class="number">73</span>, <span class="number">70</span>, <span class="number">86</span>, <span class="number">60</span>, <span class="number">7</span>]</span><br><span class="line"></span><br><span class="line">r3 = random.random()  # random <span class="built_in">float</span></span><br><span class="line">print(r3)  # <span class="number">0.48661006940942286</span></span><br><span class="line"></span><br><span class="line">r4 = random.randrange(<span class="number">6</span>)</span><br><span class="line">print(r4)  # <span class="number">5</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>statistics 模块计算数值数据的基本统计属性（均值，中位数，方差等）</p>
<figure class="highlight angelscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> statistics</span><br><span class="line"></span><br><span class="line">data = [<span class="number">2.75</span>, <span class="number">1.75</span>, <span class="number">1.25</span>, <span class="number">0.25</span>, <span class="number">0.5</span>, <span class="number">1.25</span>, <span class="number">3.5</span>]</span><br><span class="line">print(statistics.mean(data))  # <span class="number">1.6071428571428572</span></span><br><span class="line">print(statistics.median(data))  # <span class="number">1.25</span></span><br><span class="line">print(statistics.variance(data))  # <span class="number">1.3720238095238095</span></span><br></pre></td></tr></table></figure>

</li>
</ul>
<h3 id="互联网访问"><a href="#互联网访问" class="headerlink" title="互联网访问"></a>互联网访问</h3><ul>
<li><p>有许多模块可用于访问互联网和处理互联网协议</p>
</li>
<li><p>urllib.request 用于从URL检索数据</p>
<figure class="highlight livecodeserver"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">from</span> urllib.request import urlopen</span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> urlopen(<span class="string">&#x27;https://docs.python.org/zh-cn/3/tutorial/index.html&#x27;</span>) <span class="keyword">as</span> response:</span><br><span class="line">    <span class="keyword">for</span> <span class="built_in">line</span> <span class="keyword">in</span> response:</span><br><span class="line">        <span class="built_in">line</span> = <span class="built_in">line</span>.decode(<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line">        <span class="keyword">if</span> <span class="string">&#x27;2020&#x27;</span> <span class="keyword">in</span> <span class="built_in">line</span>:</span><br><span class="line">            print(<span class="built_in">line</span>)  <span class="comment"># 最后更新于 9月 29, 2020.</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
</li>
<li><p>smtplib 用于发送邮件</p>
</li>
</ul>
<h3 id="日期和时间"><a href="#日期和时间" class="headerlink" title="日期和时间"></a>日期和时间</h3><ul>
<li><p>datetime 模块提供了以简单和复杂的方式操作日期和时间的类，还支持可感知时区的对象</p>
<figure class="highlight perl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">from datetime import date</span><br><span class="line"></span><br><span class="line">now = date.today()</span><br><span class="line"><span class="keyword">print</span>(now)</span><br><span class="line"></span><br><span class="line">birthday = date(<span class="number">1964</span>, <span class="number">7</span>, <span class="number">31</span>)</span><br><span class="line">age = now - birthday</span><br><span class="line"><span class="keyword">print</span>(age.days)  <span class="comment"># 20515</span></span><br><span class="line"></span><br><span class="line">now=now.strftime(<span class="string">&quot;%m-%d-%y  %d %b %Y  %A  %d %B.&quot;</span>) <span class="comment">#09-30-20  30 Sep 2020  Wednesday  30 September.</span></span><br><span class="line"><span class="keyword">print</span>(now)</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h3 id="数据压缩"><a href="#数据压缩" class="headerlink" title="数据压缩"></a>数据压缩</h3><ul>
<li><p>常见的数据存档和压缩格式由模块直接支持，包括，zlib，gzip，bz2，zipfile 和 tarfile</p>
<figure class="highlight nsis"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">import <span class="literal">zlib</span></span><br><span class="line"></span><br><span class="line">s = b<span class="string">&#x27;witch which has which witches wrist watch&#x27;</span></span><br><span class="line"><span class="literal">print</span>(len(s))  <span class="comment"># 41</span></span><br><span class="line"></span><br><span class="line">t = <span class="literal">zlib</span>.compress(s)</span><br><span class="line"><span class="literal">print</span>(len(t))  <span class="comment"># 37</span></span><br><span class="line"></span><br><span class="line">c = <span class="literal">zlib</span>.decompress(t)</span><br><span class="line"><span class="literal">print</span>(c)  <span class="comment"># b&#x27;witch which has which witches wrist watch&#x27;</span></span><br><span class="line"></span><br><span class="line">d = <span class="literal">zlib</span>.crc32(s)</span><br><span class="line"><span class="literal">print</span>(d)  <span class="comment"># 226805979</span></span><br></pre></td></tr></table></figure>

</li>
</ul>
<h3 id="性能测量"><a href="#性能测量" class="headerlink" title="性能测量"></a>性能测量</h3><ul>
<li><p>timeit 模块可以快速演示在运行效率方面的优势</p>
<figure class="highlight reasonml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">from timeit import Timer</span><br><span class="line"></span><br><span class="line">t1 = <span class="constructor">Timer(&#x27;<span class="params">t</span>=<span class="params">a</span>; <span class="params">a</span>=<span class="params">b</span>; <span class="params">b</span>=<span class="params">t</span>&#x27;, &#x27;<span class="params">a</span>=1; <span class="params">b</span>=2&#x27;)</span>.timeit<span class="literal">()</span></span><br><span class="line">print(t1)  # <span class="number">0.030319299999999997</span></span><br><span class="line"></span><br><span class="line">t2 = <span class="constructor">Timer(&#x27;<span class="params">a</span>,<span class="params">b</span> = <span class="params">b</span>,<span class="params">a</span>&#x27;, &#x27;<span class="params">a</span>=1; <span class="params">b</span>=2&#x27;)</span>.timeit<span class="literal">()</span></span><br><span class="line">print(t2)  # <span class="number">0.028974600000000003</span></span><br></pre></td></tr></table></figure>

<p>与 timeit  的精细粒度级别相反，profile 和 pstats 模块提供了用于在较大的代码块中识别时间关键部分的工具。</p>
</li>
</ul>
<h3 id="质量控制"><a href="#质量控制" class="headerlink" title="质量控制"></a>质量控制</h3><ul>
<li><p>开发高质量软件的一种方法是在开发过程中为每个函数编写测试，并在开发过程中经常运行这些测试</p>
</li>
<li><p>doctest 模块提供了一个工具，用于扫描模块并验证程序文档字符串中嵌入的测试。测试构造就像将典型调用及其结果剪切并粘贴到文档字符串一样。这通过向用于提供示例来改进文档，并且它允许doctest模块确保代码保持对文档的真实。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">average</span>(<span class="params">values</span>):</span></span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    print(average([20,30,40]))</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="keyword">return</span> sum(values) / len(values)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> doctest</span><br><span class="line"></span><br><span class="line">doctest.testmod()</span><br></pre></td></tr></table></figure>
</li>
<li><p>unittest 模块不像 doctest 模块那样易于使用，但它允许在一个单独的文件中维护更全面的测试集：</p>
<figure class="highlight asciidoc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">import unittest</span><br><span class="line"></span><br><span class="line">class TestStatisticalFunctions(unittest.TestCase):</span><br><span class="line"></span><br><span class="line"><span class="code">    def test_average(self):</span></span><br><span class="line"><span class="code">        self.assertEqual(average([20, 30, 70]), 40.0)</span></span><br><span class="line"><span class="code">        self.assertEqual(round(average([1, 5, 7]), 1), 4.3)</span></span><br><span class="line"><span class="code">        with self.assertRaises(ZeroDivisionError):</span></span><br><span class="line"><span class="code">            average([])</span></span><br><span class="line"><span class="code">        with self.assertRaises(TypeError):</span></span><br><span class="line"><span class="code">            average(20, 30, 70)</span></span><br><span class="line"></span><br><span class="line">unittest.main()  # Calling from the command line invokes all tests</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#x27;&#x27;&#x27;</span><br><span class="line">.</span><br><span class="line">----------------------------------------------------------------------</span><br><span class="line">Ran 1 test in 0.000s</span><br><span class="line"></span><br><span class="line">OK</span><br><span class="line">&#x27;&#x27;&#x27;</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h3 id="自带电池"><a href="#自带电池" class="headerlink" title="自带电池"></a>自带电池</h3><ul>
<li>xmlrpc.client 和 xmlrpc.server 模块使得实现远程过程调用变得简单。</li>
<li>email 包是一个用于管理电子邮件的库，包括MIME和其他符合RFC 2822 规范的邮件文档。与smtplib 和 poplib 不同（它们实际上是发送和接收消息)，电子邮件包提供完整的工具集，用于构建或解码复杂的消息结构以及实现互联网编码和标头协议。</li>
<li>json 包为解析这种流行的数据交换格式提供了强大的支持。</li>
<li>csv 模块以逗号分隔值格式直接读取和写入文件，这种格式通常为数据库和电子表格所支持。</li>
<li>XML处理有 xml.etree.ElementTree，xml.dom和xml.sax 包所支持</li>
<li>sqlite3模块时SQLLite数据库库的包装器，提供了一个可以使用稍微非标准的SQL语法更新和访问的持久数据库。</li>
<li>国际化由许多模块支持，如 gettext，locale，以及 codecs 包。</li>
</ul>
<h3 id="格式化输出"><a href="#格式化输出" class="headerlink" title="格式化输出"></a>格式化输出</h3><ul>
<li><p>reprlib 模块提供了一个定制化版本的 repr() 函数，用于缩略系那是大型或深层嵌套的容器对象</p>
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">import reprlib</span><br><span class="line"></span><br><span class="line">a = reprlib.repr(<span class="builtin-name">set</span>(<span class="string">&#x27;supercalifragilisticexpialidocious&#x27;</span>))</span><br><span class="line"><span class="builtin-name">print</span>(a) # &#123;<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;f&#x27;</span>, <span class="string">&#x27;g&#x27;</span>, <span class="built_in">..</span>.&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>pprint 模块提供了更加复杂的打印控制，其输出的内置对象和用于自定义对象能够被解释器直接读取。当输出结果过长而需要拆行时，“美化输出机制”会添加换行符和索引，以更清楚地展示数据结构</p>
<figure class="highlight pgsql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pprint</span><br><span class="line"></span><br><span class="line">t = [[[[<span class="string">&#x27;black&#x27;</span>, <span class="string">&#x27;cyan&#x27;</span>], <span class="string">&#x27;white&#x27;</span>, [<span class="string">&#x27;green&#x27;</span>, <span class="string">&#x27;red&#x27;</span>]], [[<span class="string">&#x27;magenta&#x27;</span>, <span class="string">&#x27;yellow&#x27;</span>], <span class="string">&#x27;blue&#x27;</span>]]]</span><br><span class="line">pprint.pprint(t, width=<span class="number">30</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">[[[[&#x27;</span>black<span class="string">&#x27;, &#x27;</span>cyan<span class="string">&#x27;],</span></span><br><span class="line"><span class="string">   &#x27;</span>whit<span class="string">e&#x27;,</span></span><br><span class="line"><span class="string">   [&#x27;</span>green<span class="string">&#x27;, &#x27;</span>red<span class="string">&#x27;]],</span></span><br><span class="line"><span class="string">  [[&#x27;</span>magenta<span class="string">&#x27;, &#x27;</span>yellow<span class="string">&#x27;],</span></span><br><span class="line"><span class="string">   &#x27;</span>blu<span class="string">e&#x27;]]]</span></span><br><span class="line"><span class="string">&#x27;</span><span class="string">&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>textwrap 模块能够格式化文本段落，以适应给定的屏幕宽度</p>
<figure class="highlight ceylon"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> textwrap</span><br><span class="line"></span><br><span class="line"><span class="meta">doc</span> = <span class="string">&quot;&quot;&quot;The wrap() method is just like fill() except that it returns</span></span><br><span class="line"><span class="string"> a list of strings instead of one big string with newlines to separate</span></span><br><span class="line"><span class="string"> the wrapped lines. &quot;&quot;&quot;</span></span><br><span class="line">print(textwrap.fill(<span class="meta">doc</span>, width=<span class="number">80</span>))</span><br></pre></td></tr></table></figure>
</li>
<li><p>locale 模块处理与特定地域文化相关的数据格式，locale 模块的 format 函数包含一个 grouping 属性，可直接将数字格式转化为带有组分隔符的样式</p>
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">import locale</span><br><span class="line"></span><br><span class="line">a = locale.setlocale(locale.LC_ALL, <span class="string">&#x27;English_United States.1252&#x27;</span>)</span><br><span class="line"><span class="builtin-name">print</span>(a)  # English_United States.1252</span><br><span class="line"></span><br><span class="line">x = 1234567.8</span><br><span class="line">b = locale.format_string(<span class="string">&quot;%d&quot;</span>, x, <span class="attribute">grouping</span>=<span class="literal">True</span>)</span><br><span class="line"><span class="builtin-name">print</span>(b)  # 1,234,567</span><br><span class="line"></span><br><span class="line">conv = locale.localeconv()</span><br><span class="line">c = locale.format_string(<span class="string">&quot;%s%.*f&quot;</span>, (conv[<span class="string">&#x27;currency_symbol&#x27;</span>], conv[<span class="string">&#x27;frac_digits&#x27;</span>], x), <span class="attribute">grouping</span>=<span class="literal">True</span>)</span><br><span class="line"><span class="builtin-name">print</span>(c)  # <span class="variable">$1</span>,234,567.80</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h3 id="模板"><a href="#模板" class="headerlink" title="模板"></a>模板</h3><ul>
<li><p>string 模板包含一个通用的“Template” 类，具有适用于最终用户的简化语法。</p>
</li>
<li><p>格式化操作是通过占位符实现的，占位符由 $ 加上合法的Python标识符构成。一旦使用 {} 将占位符括起来，就可以在后面跟上更多的字母和数字而无需空格分割。 $$ 将被转义成单个字符 $:</p>
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> string import Template</span><br><span class="line"></span><br><span class="line">t = Template(<span class="string">&#x27;$&#123;name&#125; $role money=$$10&#x27;</span>)</span><br><span class="line">a = t.substitute(<span class="attribute">name</span>=<span class="string">&#x27;张三&#x27;</span>, <span class="attribute">role</span>=<span class="string">&#x27;学生&#x27;</span>)</span><br><span class="line"><span class="builtin-name">print</span>(a)</span><br></pre></td></tr></table></figure>
</li>
<li><p>如果在字典或关键字参数中未提供某个占位符的值，那么 substitude() 方法将抛出 KeyError</p>
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> string import Template</span><br><span class="line"></span><br><span class="line">m = Template(<span class="string">&#x27;归还 $item 给 $owner&#x27;</span>)</span><br><span class="line">n = dict(<span class="attribute">item</span>=<span class="string">&#x27;钱包&#x27;</span>)</span><br><span class="line"><span class="builtin-name">print</span>(m.substitute(n))  # KeyError: <span class="string">&#x27;owner&#x27;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>对于邮件合并类型的应用，用户提供的数据有可能是不完整的，此时使用 saft_substitute() 方法更加合适，如果数据缺失，它会直接将占位符原样保留</p>
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> string import Template</span><br><span class="line"></span><br><span class="line">m = Template(<span class="string">&#x27;归还 $item 给 $owner&#x27;</span>)</span><br><span class="line">n = dict(<span class="attribute">item</span>=<span class="string">&#x27;钱包&#x27;</span>)</span><br><span class="line"><span class="builtin-name">print</span>(m.safe_substitute(n))  # 归还 钱包 给 <span class="variable">$owner</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>Template 的子类可以自定义分隔符，例如，以下是某个照片浏览器的批量重命名功能，采用了百分号作为日期，照片序号和照片格式的占位符：</p>
<figure class="highlight pgsql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="type">time</span>, os.path</span><br><span class="line"></span><br><span class="line">photofiles = [<span class="string">&#x27;img_1074.jpg&#x27;</span>, <span class="string">&#x27;img_1076.jpg&#x27;</span>, <span class="string">&#x27;img_1077.jpg&#x27;</span>]</span><br><span class="line"><span class="keyword">class</span> BatchRename(<span class="keyword">Template</span>):</span><br><span class="line">    delimiter = <span class="string">&#x27;%&#x27;</span></span><br><span class="line"></span><br><span class="line">fmt = <span class="keyword">input</span>(<span class="string">&#x27;重命名 (%d-date %n-seqnum %f-format): &#x27;</span>)</span><br><span class="line"><span class="type">date</span> = <span class="type">time</span>.strftime(<span class="string">&#x27;%d%b%y&#x27;</span>)</span><br><span class="line"><span class="keyword">for</span> i, filename <span class="keyword">in</span> enumerate(photofiles):</span><br><span class="line">    base, ext = os.path.splitext(filename)</span><br><span class="line">    newname = BatchRename(fmt).substitute(d=<span class="type">date</span>, n=i, f=ext)</span><br><span class="line">    print(<span class="string">&#x27;&#123;0&#125; ---&gt; &#123;1&#125;&#x27;</span>.format(filename, newname))</span><br><span class="line">    </span><br><span class="line"> </span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">img_1074.jpg ---&gt; hello_0.jpg</span></span><br><span class="line"><span class="string">img_1076.jpg ---&gt; hello_1.jpg</span></span><br><span class="line"><span class="string">img_1077.jpg ---&gt; hello_2.jpg</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>

</li>
</ul>
<h3 id="使用二进制数据记录格式"><a href="#使用二进制数据记录格式" class="headerlink" title="使用二进制数据记录格式"></a>使用二进制数据记录格式</h3><ul>
<li><p>struct 模块提供了 pack() 和 unpack() 函数，用于处理不定长度的二进制记录格式。</p>
</li>
<li><p>如，在不使用 zipfile 模块的情况下，遍历循环一个ZIP文件的所有头信息。Pack代码 “H” 和 “I” 分别代表两字节和四字节无符号整数，”&lt;” 代表它们是标准尺寸的小端字节序：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">import struct</span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> <span class="keyword">open</span>(<span class="string">&#x27;demo.zip&#x27;</span>,<span class="string">&#x27;rb&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">	<span class="keyword">data</span>=f.read()</span><br><span class="line"><span class="keyword">start</span> = <span class="number">0</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="keyword">range</span>(<span class="number">3</span>):</span><br><span class="line">	<span class="keyword">start</span> += <span class="number">14</span></span><br><span class="line">	<span class="keyword">fields</span> = struct.unpack(<span class="string">&#x27;&lt;IIIHH&#x27;</span>,<span class="keyword">data</span>[<span class="keyword">start</span>:<span class="keyword">start</span>+<span class="number">16</span>])</span><br><span class="line">	<span class="keyword">crc32</span>,comp_size,uncompe_size,filenamesize,extra_size = <span class="keyword">fields</span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">start</span> += <span class="number">16</span></span><br><span class="line">	filename = <span class="keyword">data</span>[<span class="keyword">start</span>:<span class="keyword">start</span>+filenamesize]</span><br><span class="line">	<span class="keyword">start</span> += filenamesize</span><br><span class="line">	extra = <span class="keyword">data</span>[<span class="keyword">start</span>:<span class="keyword">start</span>+extra_size]</span><br><span class="line">	print(filename,<span class="keyword">hex</span>(<span class="keyword">crc32</span>),comp_size,uncomp_size)</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">start</span> += extra_size + comp_size </span><br></pre></td></tr></table></figure>

</li>
</ul>
<h3 id="多线程"><a href="#多线程" class="headerlink" title="多线程"></a>多线程</h3><ul>
<li>线程是一种对于非顺序依赖的多个任务进行解耦的技术，多线程可以提高应用的响应效率，当接收用户输入的同时，保持其他任务在后台运行。一个有关的应用场景是，将IO和计算运行在两个并行的线程中</li>
<li>多线程应用面临的主要挑战是，相互协调的多个线程之间需要共享数据或其他资源，为此，threading 模块提供了多个同步操作原语，包括线程锁，事件，条件变量和信号量。</li>
<li></li>
</ul>
<h3 id="日志记录"><a href="#日志记录" class="headerlink" title="日志记录"></a>日志记录</h3><h3 id="弱引用"><a href="#弱引用" class="headerlink" title="弱引用"></a>弱引用</h3><h3 id="用于操作列表的工具"><a href="#用于操作列表的工具" class="headerlink" title="用于操作列表的工具"></a>用于操作列表的工具</h3><h3 id="十进制浮点数运算"><a href="#十进制浮点数运算" class="headerlink" title="十进制浮点数运算"></a>十进制浮点数运算</h3>
    </div>

    
    
    

    
      <div>
       <div>
  
    <div style="text-align:center;color:#bfbfbf;font-size:16px;">
      <span>-------- 本文结束 </span>
      <i class="fa fa-snowflake-o"></i>
      <span> 感谢阅读 --------</span>
    </div>
  
</div>
      </div>
    

    <footer class="post-footer">
          <div class="reward-container">
  <div></div>
  <button onclick="document.querySelector('.post-reward').classList.toggle('active');">
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wechatpay.png" alt="shanlu 微信">
        <span>微信</span>
      </div>

  </div>
</div>

          <div class="post-tags">
              <a href="/tags/python/" rel="tag"><i class="fa fa-tag"></i> python</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2020/09/29/python/%E9%94%99%E8%AF%AF%E5%92%8C%E5%BC%82%E5%B8%B8/" rel="prev" title="错误和异常">
                  <i class="fa fa-chevron-left"></i> 错误和异常
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2020/10/01/python/python%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/" rel="next" title="python数据类型">
                  python数据类型 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
  
  
  



      
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

    </div>
  </main>

  <footer class="footer">
    <div class="footer-inner">
      

      

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">shanlu</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">225k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">3:25</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>


    <script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

    </div>
  </footer>

  
  <script size="300" alpha="0.6" zIndex="-1" src="//cdn.jsdelivr.net/npm/ribbon.js@1.0.2/dist/ribbon.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.0/lib/anime.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>




  <script src="/js/local-search.js"></script>















  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>








  

  
<script>
NexT.utils.loadComments('#valine-comments', () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js', () => {
    new Valine(Object.assign({
      el  : '#valine-comments',
      path: "/2020/09/30/%E6%95%B0%E6%8D%AE%E5%BA%93/%E6%A0%87%E5%87%86%E5%BA%93/",
    }, {"enable":true,"appId":"M0TTE46ti90I3BUUJqD5WYyD-gzGzoHsz","appKey":"UelegRsJXosPYDakAbUdO5e8","notify":true,"verify":false,"placeholder":"留下评论吧","avatar":"mm","meta":["nick","mail","link"],"pageSize":10,"lang":"zh-cn","visitor":true,"comment_count":true,"recordIP":false,"serverURLs":null,"enableQQ":false,"requiredFields":[]}
    ));
  }, window.Valine);
});
</script>

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"position":"left","width":225,"height":450},"mobile":{"show":true},"react":{"opacity":0.7},"log":false});</script></body>
</html>
